package com.fourhats.dao.impl;

import java.util.List;

import org.hibernate.SessionFactory;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import com.fourhats.dao.ItemDAO;
import com.fourhats.model.Item;

@Repository
public class ItemDAOImpl extends GenericDAOImpl<Item, Long> implements ItemDAO {

	public ItemDAOImpl() {
		super(Item.class.getName());
	}
	
	@Autowired
	private SessionFactory sessionFactory;
	
	@SuppressWarnings("unchecked")
	@Override
	@Transactional
	public List<Item> getItemsByKeyword(String keyWord) {
		DetachedCriteria criteria = DetachedCriteria.forEntityName(entityName);
        criteria.add(Restrictions.ilike("name", keyWord, MatchMode.ANYWHERE));
        criteria.add(Restrictions.eq("enable", true));
        return getHibernateTemplate().findByCriteria(criteria);
	}
}
